package com.biz.service.basic;

import com.alibaba.fastjson.JSON;
import com.biz.model.Hmodel.TBizPerson;
import com.biz.model.Hmodel.basic.*;
import com.biz.model.Pmodel.User;
import com.biz.model.Pmodel.basic.*;
import com.biz.service.base.BaseServiceImpl;
import com.framework.dao.hdao.BaseDaoI;
import com.framework.dao.mdao.DaoSupport;
import com.framework.model.MessageBox;
import com.framework.model.Pager;
import com.framework.model.Paging;
import com.framework.utils.StringUtil;
import com.framework.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by tomchen on 17/1/11.
 */
@Service("propertyService")
public class PropertyServiceImpl extends BaseServiceImpl<TGoodsBrand> implements PropertyServiceI {

    private  SimpleDateFormat  shortSdf;


    @Resource(name = "daoSupport")
    private DaoSupport dao;

    @Autowired
    private BaseDaoI<TGoodsBrand> brandDao;

    @Autowired
    private BaseDaoI<TGoodsUnit> unitDao;

    @Autowired
    private BaseDaoI<TwxGoodsGroup> groupDao;

    @Autowired
    private BaseDaoI<TWxGoodsTag> tagDao;

    @Autowired
    private BaseDaoI<TBaseStandard> standardDao;

    @Autowired
    private LogServiceI LogService;

    @Override
    public Pager<PgoodsBrand> showBrand(Pager<PgoodsBrand> pager) throws Exception {
        return dao.findForPager1("propertyDao.showBrand","propertyDao.showBrandCount",pager);
    }

    @Override
    public MessageBox deBrandByIds(String ids) throws Exception {
        MessageBox mb=new MessageBox();
        String id[]=ids.split(",");
        for(int i=0;i<id.length;i++){
            TGoodsBrand t=brandDao.getById(TGoodsBrand.class,id[i]);
            t.setIsdel((short) 1);
            brandDao.update(t);
        }
        mb.setSuccess(true);
        return mb;
    }

    @Override
    public Pager<PgoodsUnit> showUnit(Pager<PgoodsUnit> pager) throws Exception {
        return dao.findForPager1("propertyDao.showUnit","propertyDao.showUnitCount",pager);
    }

    @Override
    public TGoodsUnit getUnitById(String code) throws Exception {
        return unitDao.getById(TGoodsUnit.class,code);
    }

    @Override
    public MessageBox doSaveOrUpdateUnit(PgoodsUnit pgoodsUnit) throws Exception {

        MessageBox mb=new MessageBox();
        if(pgoodsUnit.getSource().equals("0")){
            TGoodsUnit t=new TGoodsUnit();
            t.setId(UuidUtil.get32UUID());
            t.setName(pgoodsUnit.getName());
            unitDao.save(t);
        }else{
            TGoodsUnit t=unitDao.getById(TGoodsUnit.class,pgoodsUnit.getId());
            t.setName(pgoodsUnit.getName());
            unitDao.update(t);
        }
        mb.setSuccess(true);
        return mb;
    }

    @Override
    public MessageBox deUnitByIds(String ids) throws Exception {
        MessageBox mb=new MessageBox();
        String id[]=ids.split(",");
        for(int i=0;i<id.length;i++){
            TGoodsUnit t=unitDao.getById(TGoodsUnit.class,id[i]);
            t.setIsdel((short) 1);
            unitDao.update(t);
        }
        mb.setSuccess(true);
        return mb;
    }

    @Override
    public List<PwxgoodsGroup> getAllGroupTagList(String type) throws Exception {
        return (List<PwxgoodsGroup>) dao.findForList("propertyDao.getAllGroupTagList",type);
    }

    @Override
    public PwxgoodsGroup getGroupTagDetail(String type, String id) throws Exception {
        if(type.equals("1")){
            //分组
            return (PwxgoodsGroup) dao.findForObject("propertyDao.getGroupById",id);
        }else{
            //标签
            return (PwxgoodsGroup) dao.findForObject("propertyDao.getTagById",id);
        }
    }

    @Override
    public MessageBox doSaveOrUpdateGroupTag(PwxgoodsGroup pwxgoodsGroup) throws Exception {
        MessageBox mb=new MessageBox();
        mb.setSuccess(false);
        if(pwxgoodsGroup.getState().equals("0")){
            pwxgoodsGroup.setId(UuidUtil.get32UUID());
            //新增
            if(pwxgoodsGroup.getType().equals("1")){
                //分组
                TwxGoodsGroup twxGoodsGroup=new TwxGoodsGroup();
                twxGoodsGroup.setId(pwxgoodsGroup.getId());
                twxGoodsGroup.setName(pwxgoodsGroup.getName());
                twxGoodsGroup.setNote(pwxgoodsGroup.getNote());
                twxGoodsGroup.setIcon(pwxgoodsGroup.getIcon());
                groupDao.save(twxGoodsGroup);
            }else{
                //标签
                TWxGoodsTag tWxGoodsTag=new TWxGoodsTag();
                tWxGoodsTag.setId(pwxgoodsGroup.getId());
                tWxGoodsTag.setName(pwxgoodsGroup.getName());
                tWxGoodsTag.setNote(pwxgoodsGroup.getNote());
                tWxGoodsTag.setIcon(pwxgoodsGroup.getIcon());
                tagDao.save(tWxGoodsTag);
                //新增关联表
                pwxgoodsGroup.setCorrelateId(UuidUtil.get32UUID());
                dao.save("propertyDao.saveCorrelate",pwxgoodsGroup);
            }
        }else{
            //编辑
            if(pwxgoodsGroup.getType().equals("1")){
                //分组
                TwxGoodsGroup twxGoodsGroup=groupDao.getById(TwxGoodsGroup.class,pwxgoodsGroup.getId());
                twxGoodsGroup.setName(pwxgoodsGroup.getName());
                twxGoodsGroup.setNote(pwxgoodsGroup.getNote());
                twxGoodsGroup.setIcon(pwxgoodsGroup.getIcon());
                groupDao.update(twxGoodsGroup);
            }else{
                //标签
                TWxGoodsTag tWxGoodsTag=tagDao.getById(TWxGoodsTag.class,pwxgoodsGroup.getId());
                tWxGoodsTag.setName(pwxgoodsGroup.getName());
                tWxGoodsTag.setNote(pwxgoodsGroup.getNote());
                tWxGoodsTag.setIcon(pwxgoodsGroup.getIcon());
                tagDao.update(tWxGoodsTag);
            }

        }
        mb.setSuccess(true);
        return mb;
    }

    @Override
    public MessageBox delgroupTag(String id, String pid) throws Exception {
        MessageBox mb=new MessageBox();
        mb.setSuccess(false);
        if(pid.equals("0")){
            //删除分组
            TwxGoodsGroup twxGoodsGroup=groupDao.getById(TwxGoodsGroup.class,id);
            twxGoodsGroup.setIsdel((short)1);
            groupDao.update(twxGoodsGroup);
            //获取所有标签值
            List<String> list=(List<String>) dao.findForList("propertyDao.getAllTagById",id);
            String ids="";
            for(int  i=0;i<list.size();i++){
                ids+=list.get(i)+",";
            }
            if(!ids.equals("")) {
                //删除关联表
                dao.update("propertyDao.delCorrelate", StringUtil.formatSqlIn(ids));
                //删除子表
                dao.update("propertyDao.delTagById", StringUtil.formatSqlIn(ids));
            }
        }else{
            //标签
            TWxGoodsTag tWxGoodsTag=tagDao.getById(TWxGoodsTag.class,id);
            tWxGoodsTag.setIsdel((short)1);
            tagDao.update(tWxGoodsTag);
        }
        //删除关联表
        dao.update("propertyDao.delCorrelate", StringUtil.formatSqlIn(id));
        mb.setSuccess(true);
        return mb;
    }

    @Override
    public List<PbaseStandard> getAllStandardList(String type) throws Exception {
        return (List<PbaseStandard>) dao.findForList("propertyDao.getAllStandardList",type);
    }

    @Override
    public PbaseStandard getPbaseStandard(String id) throws Exception {
        return (PbaseStandard) dao.findForObject("propertyDao.getPbaseStandard",id);
    }

    @Override
    public MessageBox doSaveOrUpdateStandard(PbaseStandard pbaseStandard) throws Exception {
        MessageBox mb=new MessageBox();
        if(pbaseStandard.getType().equals("0")){
            //新增
            TBaseStandard tBaseStandard=new TBaseStandard();
            tBaseStandard.setId(UuidUtil.get32UUID());
            tBaseStandard.setTypeId("");
            tBaseStandard.setText(pbaseStandard.getText());
            tBaseStandard.setPid(pbaseStandard.getPid());
            standardDao.save(tBaseStandard);
        }else{
            //编辑
            TBaseStandard tBaseStandard=standardDao.getById(TBaseStandard.class,pbaseStandard.getId());
            tBaseStandard.setPid(pbaseStandard.getPid());
            tBaseStandard.setText(pbaseStandard.getText());
            standardDao.update(tBaseStandard);
        }
        mb.setSuccess(true);
        return mb;
    }

    @Override
    public MessageBox delStandard(String id) throws Exception {
        MessageBox mb=new MessageBox();
        TBaseStandard tBaseStandard=standardDao.getById(TBaseStandard.class,id);
        tBaseStandard.setIsdel((short) 1);
        standardDao.update(tBaseStandard);
        //删除该sku下面的子类
        dao.update("propertyDao.delChildStandard",id);
        mb.setSuccess(true);
        return mb;
    }
}
